package club.banyuan.hrms.servlet;

import club.banyuan.hrms.dao.AdminDao;
import club.banyuan.hrms.dao.impl.AdminDaoImpl;
import club.banyuan.hrms.dao.impl.AdminDbDaoImpl;
import club.banyuan.hrms.domain.Admin;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
//@WebServlet(name = "AdminListServlet",urlPatterns = "/admin/list")
public class AdminListServlet extends HttpServlet {
    private AdminDao adminDao;

    // 自动注入
    @Autowired
    public AdminListServlet(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    public AdminDao getAdminDao() {
        return adminDao;
    }

//    解决冲突问题 使用Qualifier 但是这个注解不能在构造器上面     如果用这种方法，要把有参构造方法删除
//    @Autowired
//    @Qualifier("AdminDbDaoImpl")

    //    @Inject
//    @Named("AdminDbDaoImpl")
    public void setAdminDao(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    @Override
    public void init() throws ServletException {
//        adminDao = new AdminDaoImpl();
//        adminDao = new AdminDbDaoImpl();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        // 获取查询参数
        String id = request.getParameter("id");
        String username = request.getParameter("username");
        String rolename = request.getParameter("rolename");
        String password = request.getParameter("password");

        Admin admin = new Admin();
        admin.setId((id != null) ? Integer.parseInt(id) : null);
//        admin.setId((!id.equals("null")) ? Integer.parseInt(id) : null);
        admin.setUsername(username);
        admin.setRolename(rolename);
        admin.setPassword(password);


        // 判断修改还是增加，调用修改或者增加的方法
        if (id != null) {
            adminDao.updateAdmin(admin);
        } else {
            adminDao.addAdmin(admin);
        }

        // 返回列表页面
        response.sendRedirect(request.getContextPath() + "/adminManage.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 返回的数据是json格式，设置ContentType
        response.setContentType("application/json;charset=utf-8");

        List<Admin> admins = adminDao.getAdmins();

        // 按照接口要求返回数据
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("total", admins.size());
        resultMap.put("code", 0);
        resultMap.put("rows", admins);
        response.getWriter().println(JSONObject.toJSONString(resultMap));
    }
}
